package com.dpj.noLock.unsafe;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

public class UnsafeInteger {

}
class  MyAtomicInteger {
    private  volatile int value;
    private static   long valueOffset;
    static final Unsafe UNSAFE;
    static {
        UNSAFE=UnsafeAccessor.getUnsafe();
        try {
            valueOffset= UNSAFE.objectFieldOffset(MyAtomicInteger.class.getDeclaredField("value"));
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }

    public int getValue(){
        return value;
    }

    public void  decrement(int amount){
        while (true){
            int pre=this.value;
            int next=pre-amount;
            if (UNSAFE.compareAndSwapInt(this,valueOffset,pre,next)) {
                  break;
            }
        }
    }


}
